﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Web.Enhancements.Rest.Templates
{
    /// <summary>
    /// This class returns result from REST service method using template post-processing; 
    /// that is applying data source data to the HTML Template and sending resulted rendering as response back to client.
    /// </summary>
    /// <typeparam name="TDataSource">Generic object type of DataSource for the HTML Template.</typeparam>
    public class TemplatedResult<TDataSource> : ITemplatedResult
    {
        /// <summary>
        /// Gets or sets data source for the HTML Template.
        /// </summary>
        public TDataSource DataSource { get; set; }
        /// <summary>
        /// Gets or sets virtual path for the HTML Template.
        /// </summary>
        public string VirtualPath { get; set; }
        /// <summary>
        /// Gets collection of temporary items used to pass additional data to HTML template.
        /// </summary>
        public Dictionary<string, object> TempData { get; private set; }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public TemplatedResult()
        {
            this.TempData = new Dictionary<string, object>();
            this.VirtualPath = "";
        }

        /// <summary>
        /// Class constructor that accepts virtual path for the HTML template and generic data source.
        /// </summary>
        /// <param name="virtualPath">Virtual path for the HTML template.</param>
        /// <param name="dataSource">Data source.</param>
        public TemplatedResult(string virtualPath, TDataSource dataSource)
            : this()
        {
            this.VirtualPath = virtualPath;
            this.DataSource = dataSource;
        }

        /// <summary>
        /// Class constructor that accepts virtual path for the HTML template
        /// </summary>
        /// <param name="virtualPath">Virtual path for the HTML template.</param>
        public TemplatedResult(string virtualPath)
            : this()
        {
            this.VirtualPath = virtualPath;
        }

        /// <summary>
        /// Method renders result's content into given HTTP response object.
        /// </summary>
        /// <param name="response">HTTP response object.</param>
        public void RenderResult(System.Web.HttpResponse response)
        {
            Html32TextWriter writer = new Html32TextWriter(response.Output);
            HtmlTemplateFactory.RenderTemplate<TDataSource>(VirtualPath, writer, DataSource, TempData);
        }

        /// <summary>
        /// Method renders result's content into given HTML writer object.
        /// </summary>
        /// <param name="writer">Html Text Writer object to write result to.</param>
        public void RenderResult(HtmlTextWriter writer)
        {
            HtmlTemplateFactory.RenderTemplate<TDataSource>(VirtualPath, writer, DataSource, TempData);
        }
    }

    /// <summary>
    /// This class returns result from REST service method using template post-processing; 
    /// that is applying data source data to the HTML Template and sending resulted rendering as response back to client.
    /// </summary>
    public class TemplatedResult : TemplatedResult<object>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TemplatedResult()
            : base()
        {
        }
    }
}
